/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.kipme.connectors;

import java.util.Date;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.kipme.department.Order;
import org.kipme.department.OrderStatus;
import org.kipme.department.Product;
import org.kipme.department.ProductOrder;
import org.kipme.entities.Magazinedproducts;
import org.kipme.entities.Orderedproducts;
import org.kipme.entities.Orders;

/**
 *
 * @author Domyślny
 */
public class OrderManager {
    
    public static void SaveOrders(List<Order> ordersList){
//        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
//        
//        for (int i = 0; i < ordersList.size(); i++) {
//            Order order = ordersList.get(i);
//            
//            if(OrderManager.CheckIfOrderIsInDatabase(order.getOrderId())){
//                continue;
//            }
//            
//            Date date = new Date();
//            Orders orders = new Orders(date, order.getOrderId());
//            session.beginTransaction();
//            session.save(orders);
//            
//            for (int j = 0; j < order.getProducts().size(); j++) {
//                ProductOrder productOrder = order.getProducts().get(j);
//                
//                if(!OrderManager.CheckIfProductIsInMagazine(productOrder.getProductId())){
//                    Product prod = DepartmentWSConnector.getProductByProductCode(productOrder.getProductId());
//                    //public Magazinedproducts(String name, String status, String type, String category, Integer amount, String description, String productCode) {
//                    Magazinedproducts newProduct = new Magazinedproducts(prod.getName(), OrderStatus.EXPECTANT.toString(), prod.getType().toString(), prod.getCategory().toString(), 0, prod.getDescribe(), prod.getProductCode());
//                    session.beginTransaction();
//                    session.save(newProduct);                  
//                }
//                else{
//                    Magazinedproducts prod = OrderManager.getMagazinedproducts(productOrder.getProductId());
//                    prod.setStatus(OrderStatus.EXPECTANT.toString());
//                    session.beginTransaction();
//                    session.update(prod);
//                }
//                
//                session.getTransaction().commit();
//                
//                Orderedproducts orderedProducts = new Orderedproducts(new OrderedproductsId(orders.getId(), OrderManager.getMagazinedproductsId(productOrder.getProductId()), productOrder.getNumberOfProduct()));
//                session = NewHibernateUtil.getSessionFactory().getCurrentSession();
//                session.beginTransaction();
//                session.save(orderedProducts);
//            }            
//        }
//        
//        
//        session.getTransaction().commit();
    }
    
    public static boolean CheckIfOrderIsInDatabase(String departmentOrderId){
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Query q = session.createQuery("from Orders where departmentorderid = " + "'" + departmentOrderId + "'");
        if(q.list().size() == 1){
            return true;
        }
        
        return false;
    }
    
    public static boolean CheckIfProductIsInMagazine(Magazinedproducts product){
        
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Query q = session.createQuery("from Magazinedproducts where productcode = " + "'" + product.getProductcode() +"' and category = '" + product.getCategory() + "' and type = '" + product.getType() + "'");
        if(q.list().size() == 1){
            return true;
        }
        
        return false;
    }
    
    public static boolean CheckIfProductIsInMagazine(String productCode){
        
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Query q = session.createQuery("from Magazinedproducts where productcode = " + "'" + productCode +"'");
        if(q.list().size() == 1){
            return true;
        }
        
        return false;
    }
    
    public static int getMagazinedproductsId(String productCode){
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Query q = session.createQuery("from Magazinedproducts where productcode = " + "'" + productCode +"'");
        if(q.list().size() == 1){
            return ((Magazinedproducts)q.list().get(0)).getId();
        }
        
        return -1;
    }
    
    public static Magazinedproducts getMagazinedproducts(String productCode){
        
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Query q = session.createQuery("from Magazinedproducts where productcode = " + "'" + productCode +"'");
        if(q.list().size() == 1){
            return (Magazinedproducts)q.list().get(0);
        }
        
        return null;
    }
    
}
